<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
<p>
	<input type="button" id="enter" value="发消息给Native" onclick="testClick()" />
	<input type="button" id="enter" value="发消息给Native" onclick="testClick1()" />
</p>
</body>

<script>
		
		(function(){
			if(window.JsBridge){
				return;
			}

			var id = 0;
			
			var jsCallNativeSuccessCallback = {};
			var jsCallNativeFailCallback = {};
			
			
			/**
			 * 处理数据
			 * @param {Object} params 数据
			 */
			function dataToString(params) {
				var resultParams;
				if (typeof params === 'undefined' || typeof params === 'function') {
					resultParams = "";
				} else if (typeof params === 'object') {
					try {
						resultParams = JSON.stringify(params);
					} catch (e) {
						console.log(e);
						resultParams = "";
					}
				} else {
					resultParams = params;
				}
				return resultParams;
			}

			/**
			 * 生成回调ID
			 * @param {Object} methodName 方法名字符串
			 */
			function getCallbackId(methodName) {
				var timestamp = new Date().getTime();
				id++;
				return String(methodName + timestamp + id);
			}

			function handleCallbackDataAndParams(params, cbId) {
				//参数
				var paramsData = "";
				if (params && params.length) {
					//有参数
					if (params.length == 1 && typeof params[0] != 'function') {
						paramsData = dataToString(params[0]);
					} else if (params.length == 1 && typeof params[0] === 'function') {
						jsCallNativeSuccessCallback[cbId] = params[0];
					} else if (params.length == 2 && typeof params[0] != 'function' && typeof params[1] === 'function') {
						paramsData = dataToString(params[0]);
						jsCallNativeSuccessCallback[cbId] = params[1];
					} else if (params.length == 2 && typeof params[0] === 'function' && typeof params[1] === 'function') {
						jsCallNativeSuccessCallback[cbId] = params[0];
						jsCallNativeFailCallback[cbId] = params[1];
					} else if (params.length == 3 && typeof params[0] != 'function' && typeof params[1] === 'function' && typeof params[
							2] === 'function') {
						paramsData = dataToString(params[0]);
						jsCallNativeSuccessCallback[cbId] = params[1];
						jsCallNativeFailCallback[cbId] = params[2];
					}
				}
				return paramsData;
			}

			window.JsBridge = {
				toast: function() {
					var cbId = getCallbackId("toast");
					var paramsData = handleCallbackDataAndParams(arguments, cbId);
					return window.JsWebViewBridge.nativeDispatch("com.like.jsbridge.JsBridgeToast", "nativeArgAndCallback", paramsData, cbId);
				},
				on: function(cbId, result, deleteId) {
					console.log("cbId:" + cbId);
					console.log("返回结果：" + JSON.stringify(result));
					var success = jsCallNativeSuccessCallback[cbId];
					var fail = jsCallNativeFailCallback[cbId];
					try {
						var resultData = result;
						if (typeof result == 'string') {
							resultData = JSON.parse(result);
						}
						if (resultData.code == 0 && success) {
							success(resultData);
						}
						if (resultData.code != 0 && fail) {
							fail(resultData);
						}
						if (deleteId) {
							delete jsCallNativeSuccessCallback[cbId];
							delete jsCallNativeFailCallback[cbId];
						}
					} catch (e) {
						throw new Error("回调数据异常");
					}
				},
				dataToString:dataToString
			}

		})();


		function testClick() {
			var result = window.JsBridge.toast(1111, function(data) {
			 	// alert(JSON.stringify(data))
			})

			alert(result)
		}

		function jsNoArgAndNoCallback(params,callback){
			alert(JSON.stringify(params))
			callback(2345)
		}
		
	</script>

</html>
